package com.bj58.waf;
import com.bj58.dsap.api.scf.contract.Condition;
import com.bj58.dsap.api.scf.contract.IHbaseApi;
import com.bj58.dsap.api.scf.contract.Result;
import com.bj58.dsap.api.scf.module.Put;
import com.bj58.spat.scf.client.SCFInit;
import com.bj58.spat.scf.client.proxy.builder.ProxyFactory;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class App {
    public final static String url = "tcp://dsapapi6/HbaseApi";
    public final static String Htable = "hdp_teu_op:waf";
    public final static String Hcolumn = "data";
    public static void main(String[] args) throws Exception {
        SCFInit.initScfKey("./scfkey.key");
        IHbaseApi service = null;
        service = ProxyFactory.create(IHbaseApi.class, url);
        query1(service, Htable, "e10adc3949ba59abbe56e057f20f883e");
        /*
        List<Put> P1 = new ArrayList<Put>();
        Put put1 = new Put();
        put1.setColumnName("test");
        put1.setColumnValue("8786");
        put1.setRowKey("e10adc3949ba59abbe56e057f20f883e");
        P1.add(put1);
        */

        //puts(Htable,Hcolumn, P1);
        //query("waf_bhase", "01056355c4e20ade09b58ff957e3f6e", null, "01091407f834e2b6d114a6fbeafcc64", 10);
        //service.insert("waf_hbase", "data", P1);
    }

    private static void query1(IHbaseApi service, String tablename, String rowkey) {
        Result result = service.queryByRowKey(tablename, rowkey);
        System.out.println(result.getScfMap());
    }

    /*
    private static void deleteRows(IHbaseApi service, String tablename, List<String> rowkeys) {
        service.deleteRows(tablename, rowkeys);
    }
    */

    private static void query(String tablename, String startkey,
                              String offsetkey, String endkey, int pagesize) {
        IHbaseApi service = null;
        service = ProxyFactory.create(IHbaseApi.class, url);
        List<Result> result = null;
        String table = tablename;
        String startk = startkey;
        String offsetk = offsetkey;
        String endk = endkey;
        int pages = pagesize;
        int retry = 3;
        for (int i = 0; i < retry; i++) {
            try {
                result = service.nextPagesByKeyInterval(table, startk, offsetk, endk, pages);
                if (result == null) {
                    System.out.println("dsapapi service dsiable ...");
                } else {
                    Iterator iteratorA = result.iterator();
                    while(iteratorA.hasNext()) {
                        Object resultkv11 = iteratorA.next();
                        System.out.println(((Result) resultkv11).getScfMap());
                    }
                }
                break;
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }

    public static void puts(String tablename, String family, List<Put> put) {
        IHbaseApi service = null;
        service = ProxyFactory.create(IHbaseApi.class, url);
        String tab = tablename;
        String fam = family;
        List<Put> p = put;
        try {
            service.insert(tab, fam, p);
            System.out.println("puts success");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}